﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Goodway.Dao.Utility;
using System.Xml.Linq;

namespace ExcelImportComponent.Valider.Implement
{
    public class ValidLength : IVaild
    {
        #region IVaild 成员

        public List<ExcelImportValidMessage> DoValid(string validColumnName, List<Goodway.Dao.Utility.GoodwayEntity> entities, XElement validSetting, string schemaname)
        {
            int length = 0;
            if (string.IsNullOrEmpty(validSetting.Attribute("Length").Value)) throw new Exception("进行长度验证时 必须设定长度参数");
            if (!int.TryParse(validSetting.Attribute("Length").Value, out length)) throw new Exception("长度参数必须为整数");
            string message = string.Format("长度超过{0}", length.ToString());

            List<ExcelImportValidMessage> returnValue = new List<ExcelImportValidMessage>();
            for (int i = 0; i < entities.Count; i++)
            {
                GoodwayEntity entity = entities[i];
                if (entity[validColumnName] != null && entity[validColumnName].Length > length)
                {
                    ExcelImportValidMessage objMessage = new ExcelImportValidMessage(i.ToString(), validColumnName);
                    objMessage.Value = entity[validColumnName];
                    objMessage.ErrorMessage.Add(message);
                    returnValue.Add(objMessage);
                }
            }
            return returnValue;
        }

        #endregion
    }
}
